#!C:/strawberry/perl/bin/perl.exe

use strict;
use warnings;

use CGI;
use RPC::XML;
use RPC::XML::Client;
#use XML::Dumper;
use Data::Dumper;
use XML::Simple;
use XML::eXistDB::RPC;
use XML::Compile::RPC::Client;

# read the CGI params
my $cgi = CGI->new;
my $correo = $cgi->param("correo");
my $password = $cgi->param("password");
#my $nombre = 'nombre';
#my $password = 'password';
my $eXist = XML::eXistDB::RPC->new(destination=>"http://localhost:8080/exist/xmlrpc/", repository=>"/db", user=>"admin", password=>"eaf2julian" ) ;

my $xquery = 'doc("/placeanddate/usuarios.xml")/usuarios/usuario[email/text() eq "'.$correo.'" and password/text() eq "'.$password.'"]' ;
my ($rc1, $set) = $eXist->executeQuery($xquery) ;
my ($rc2, $count) = $eXist->numberOfResults($set) ;
#print Dumper($count) ;
my ($rc3, @data) = $eXist->retrieveResults($set) ;
$eXist->releaseResultSet($set) ;
#print Dumper(@data) ;

my $json="";
if($count==1)
{
        my $nombre=$data[0]{'usuario'}{'nombre'};
        # create a JSON string according to the database result
	$json =qq{{"hits" : "$count", "nombre" : "$nombre" ,"correo" : "$correo"}};
}
else
{
        # create a JSON string according to the database result
	$json =qq{{"hits" : "$count"}};
}

# return JSON string
print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json;